﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net;
using System.IO;
using System.Data.SqlClient;
namespace QuanLyNhaSach
{
    public partial class frmBackupRestore : Form
    {
        //Tao Parttern Seleton
        #region tao bien'
        static frmBackupRestore doituong = null;
        sv_QuanLyNhaSach.AdapterClient service = new sv_QuanLyNhaSach.AdapterClient();
        private ILog logger;
        #endregion

        public static frmBackupRestore TaoDoiTuong()
        {
            if (doituong == null)
                doituong = new frmBackupRestore();
            return doituong;
        }

        public frmBackupRestore()
        {
            InitializeComponent();
        }

        string Filename = "";
        string Filenames = "";
        private void frmBackupRestore_Load(object sender, EventArgs e)
        {
            logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            try
            {

            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }

        DataSet ds_tam = new DataSet();
        private void bt_thuchien_Click(object sender, EventArgs e)
        {

            if (txt_duongdan.Text.Trim().Length == 0)
            {
                MessageBox.Show("Bạn chưa xác định đường dẫn lưu file", "Thông bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (rdb_SaoLuu.Checked == true)// backup
            {
                try
                {
                    progressBar1.Value = 0;
                    progressBar1.Maximum = 100;
                    progressBar1.Value = 10;
                    string chuoi_NgayThang = DateTime.Now.Day.ToString("00") + DateTime.Now.Month.ToString("00") + DateTime.Now.Year.ToString();
                    service.selectqueryByText("backup database QLNhaSach to disk = '" + txt_duongdan.Text.Trim() + "\\QLNhaSach_" + chuoi_NgayThang + ".bak'");
                    progressBar1.Value += 10;
                    progressBar1.Value = 50;
                }
                catch (Exception)
                {
                    MessageBox.Show("Thất bại khi Backup");
                    this.progressBar1.Value = 0;
                    return;
                }
                this.progressBar1.Value = 90;
                this.progressBar1.Value = 100;
                MessageBox.Show("Sao luu dữ liệu thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.progressBar1.Value = 0;
            }
            else if (rdb_phuchoi.Checked == true) //restore
            {
                try
                {
                    progressBar1.Value = 0;
                    progressBar1.Maximum = 100;
                    progressBar1.Value = 10;
                    string duong_dan = txt_duongdan.Text;
                    if (!service.Restore_Data("Alter database QLNhaSach Set MULTI_USER WITH ROLLBACK IMMEDIATE " +
                                        "restore database QLNhaSach from disk = '" + duong_dan + "' with replace"))
                    {
                        //MessageBox.Show("Retore thất bại! Vui lòng chọn lại đúng đường dẫn", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //this.progressBar1.Value = 0;
                        //return;
                    }
                    progressBar1.Value += 10;
                    progressBar1.Value = 50;
                }
                catch
                {
                    MessageBox.Show("LỖi khi Restore cơ sở dử liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.progressBar1.Value = 0;
                    return;

                }
                this.progressBar1.UseWaitCursor = true;
                this.progressBar1.Value = 90;
                this.progressBar1.Value = 100;
                this.progressBar1.UseWaitCursor = false;
                MessageBox.Show("Retore thành công !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.progressBar1.Value = 0;

            }
        }

        private void bt_duongdan_Click(object sender, EventArgs e)
        {
            if (rdb_SaoLuu.Checked == true)
            {

                if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                {
                    txt_duongdan.Text = folderBrowserDialog1.SelectedPath;
                }                                  
            }
            else
            {
                openFileDialog1.Filter = "Text files(*.bak)|*.bak";
                openFileDialog1.Title = "Mở tập tin ";
                openFileDialog1.FileName = "";
                //bat dau tinh tu 1
                openFileDialog1.FilterIndex = 1;
                if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
                {
                    return;
                }
                else
                {
                    Filenames = openFileDialog1.FileName;
                    txt_duongdan.Text = Filenames;
                }
            }
        }

        private void bt_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void rdb_SaoLuu_CheckedChanged(object sender, EventArgs e)
        {
            radGroupBox1.Text = "SAO LƯU DỮ LIỆU";
            txt_duongdan.Text = "";
        }

        private void rdb_phuchoi_CheckedChanged(object sender, EventArgs e)
        {
            radGroupBox1.Text = "PHỤC HỒI DỮ LIỆU";
            txt_duongdan.Text = "";
        }


    }
}
